package workers;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.JOptionPane;

import messaging.IMessagingHandler;
import messaging.JMSMessagingConstants;
import messaging.SoftwareDevelopmentMessagingHandler;
import model.Software;
import workerUIs.StepFrame;

public class TestStepWorker extends AbstractStepSwingWorker {
	
	public TestStepWorker(StepFrame frame,
			IMessagingHandler<Software> messagingHandler) {
		super(frame, messagingHandler);
	}

	@Override
	protected Software doInBackground() throws Exception {
		return super.doInBackground();
	}
	
	@Override
	protected void doStep(Software software) {		
		//This step does not modify the Software object.
	}
	
	public static void main(String[] args)
	{
		Logger logger = Logger.getLogger(TestStepWorker.class.getCanonicalName());
		
		try {			
			final StepFrame testFrame = new StepFrame("If block: Testing", 1200, 600);			
			List<String> testInputQueues = new ArrayList<String>();
			testInputQueues.add(JMSMessagingConstants.INTEGRATION_OUTPUT_TEST_INPUT);
			List<String> testOutputQueues = new ArrayList<String>();
			testOutputQueues.add(JMSMessagingConstants.TEST_OUTPUT_REPAIR_INPUT);
			testOutputQueues.add(JMSMessagingConstants.RELEASE_INPUT);
			final IMessagingHandler<Software> messagingHandler = new SoftwareDevelopmentMessagingHandler(testFrame, testInputQueues, testOutputQueues);
			testFrame.getNextButton().addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent arg0) {
					/**
					 * User decision: if tests were successful, then the next step will be 'Release Step'.
					 * 				  Otherwise, next step is 'Repair Step', and 'Release Step' comes after it.
					 */
					String outputQueueName = null;
					switch (JOptionPane.showConfirmDialog(testFrame, "Tests succcessful?", "Tests succcessful?", JOptionPane.YES_NO_OPTION)) {
					case JOptionPane.YES_OPTION: 
						outputQueueName = JMSMessagingConstants.RELEASE_INPUT;
						break;
					default: 
						outputQueueName = JMSMessagingConstants.TEST_OUTPUT_REPAIR_INPUT;
						break;
					}
					messagingHandler.setTargetQueue(outputQueueName);
					new TestStepWorker(testFrame, messagingHandler).execute();				
				}
			});
		}
		catch (Exception e) {
			logger.log(Level.SEVERE, "Error during initialization: ", e);
		}
	}

}
